package com.infore.dataAudit.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.infore.dataAudit.entity.TFunDictionary;
import com.infore.dataAudit.repository.ITFunDictionaryRepository;


/**
 * 流程参数读取工具类
 * @author jht
 *
 */
@Component
public class ActivitiVarUtil {
    
    @Autowired
    private ITFunDictionaryRepository funDictionaryRepository;
    
    /**
     * 数据字典中配置的键值对参数，键值均为字符串类型。
     * 
     * @author cys
     * @return Map<String,Object>
     * @param pId
     * @date 2018年6月22日上午11:21:29
     */
    public Map<String, Object> getVarsFromDictionary(Integer pId){
        
        List<TFunDictionary> vars = funDictionaryRepository.findByPid(pId);
        if(vars.isEmpty()) {
            return Collections.emptyMap();
        }
        Map<String, Object> result = new HashMap<>();
        for (TFunDictionary d : vars) {
            String k_v = d.getReValue();
            String[] arr = k_v.split("=");
            if(arr.length != 2) {
                continue;
            }
            if(arr[0].trim().endsWith("List")) {
                List<String> values = Arrays.asList(arr[1].trim().split(","));
                result.put(arr[0].trim(), new ArrayList<String>(values));
            }else {
                result.put(arr[0].trim(), arr[1].trim());
            }
        }
        return result;
    }
    
    
}
